package com.belarus.kamianiec.photo.photoSite.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.belarus.kamianiec.photo.photoSite.domain.Gallery;
import com.belarus.kamianiec.photo.photoSite.domain.News;

@Repository
public class GalleryDAOImpl implements GalleryDAO {

	private SessionFactory sessionFactory;

	public void addGallery(Gallery gallery) {
		String sql = "";
		// String sql = "SELECT * FROM NEWS WHERE rnumber = '" +
		// news.getRnumber() + "'";
		List<News> answer = sessionFactory.getCurrentSession().createSQLQuery(sql).addEntity(Gallery.class).list();
		if (answer.isEmpty()) {
			sessionFactory.getCurrentSession().save(gallery);
		}
	}

	@SuppressWarnings("unchecked")
	public List<Gallery> listGallery() {
		return sessionFactory.getCurrentSession().createQuery("from Gallery").list();
	}

	public void removeGallery(Integer id) {
		Gallery gallery = (Gallery) sessionFactory.getCurrentSession().load(Gallery.class, id);
		if (null != gallery) {
			sessionFactory.getCurrentSession().delete(gallery);
		}
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Autowired(required=false)
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
}
